{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ❇️ argsort 🤜 🤛 argpartition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's say we want the smallest 3 numbers in array 👇 \n",
    "# in any order\n",
    "arr = np.array([4, 5, 6, 7, 1, 2, 3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ❇️ Method 1: argsort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "indices: [4 5 6 0 1 2 3]\n",
      "smallest 3 elements: [1 2 3]\n"
     ]
    }
   ],
   "source": [
    "# argsort, Returns the indices that would sort an array.\n",
    "idx = np.argsort(arr)\n",
    "print(f'indices: {idx}')\n",
    "print(f'smallest 3 elements: {arr[idx[:3]]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ❇️ Method 2: argpartition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "indices: [4 6 5 0 1 2 3]\n",
      "smallest 3 elements: [1 3 2]; ⬅️ notice the order\n"
     ]
    }
   ],
   "source": [
    "# It takes an array and the element index (kth) to partiotion by\n",
    "# The k-th element will be in its final sorted position and all smaller \n",
    "# elements will be moved before it and all larger elements behind it.\n",
    "# order of all the elements in each partition is undefined.\n",
    "idx = np.argpartition(a = arr, kth = 3)\n",
    "print(f'indices: {idx}')\n",
    "print(f'smallest 3 elements: {arr[idx[:3]]}; ⬅️ notice the order')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  ❇️ Why use argpartition ⁉️\n",
    "##### ❇️ If the order in is not necessary then argpartition is faster 🏎💨 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.53 ms ± 24.6 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "xs = np.random.normal(size=56000)\n",
    "np.argpartition(xs, 10)[:10]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.76 ms ± 37.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "xs = np.random.normal(size=56000)\n",
    "np.argsort(xs)[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ❇️ Cheers!! 🍺 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_twitter",
   "language": "python",
   "name": "env_twitter"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
